# -*- coding: utf-8 -*-
from parse_cwbl import *
import bsddb, urllib,time
from BeautifulSoup import BeautifulSoup 
from parse_cwbl import *

d = bsddb.btopen('../data/c2n.bsd', 'r')
#print len(d)
u6 = "http://share.jrj.com.cn/cominfo/cwbl_601398.htm"
error_s = unicode("您输入了错误的股票代码",'u8')
#d.set_location('510052')

def exact_info(results):
    d = {}
    for table in results:
        for row in table[1:]:
            d[row[0]] = row[1]
    return d

i = 0
while True:
    try:c  = d.next()
    except KeyError:break
    #print '\n',c[0],c[1]

    u = "http://share.jrj.com.cn/cominfo/cwbl_%s.htm"%c[0]
    
    soup = BeautifulSoup(urllib.urlopen(u), fromEncoding='gb18030')
    if soup('title')[0].string == unicode('Error', 'u8'):
        continue
        
    e = soup.findAll(text=lambda(x): x.startswith(error_s))
    if e:
        #print e[0]
        continue
    
    i = i + 1
    #print i

    res = exact_info(cwbl(u))
    #print res 
    ks = res.keys() 
    ks.sort()

    if i == 1: 
        print '代码','\t','股票名','\t',
        for k in ks:print k,'\t',
        print 

    print c[0],'\t',c[1],'\t',

    for k in ks:
        item = res[k]
        if item:print item,'\t',
        else:
            print '','\t',
    print

 	 
